<template>
  <div id="aCoursesList" class="bg-fa of">
    <!-- /课程列表 开始 -->
    <section class="container">
      <header class="comm-title">
        <h2 class="fl tac">
          <span class="c-333">全部课程</span>
        </h2>
      </header>
      <section class="c-sort-box">
        <section class="c-s-dl">
          <dl>
            <dt>
              <span class="c-999 fsize14">课程类别</span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li v-for="(item, index) in oneSubjectList" :key="index">
                  <a :title="item.title" :class="{active: oneIndex === index}" @click="searchOneSubject(item.id, index)">{{item.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <dl>
            <dt>
              <span class="c-999 fsize14"></span>
            </dt>
            <dd class="c-s-dl-li">
              <ul class="clearfix">
                <li v-for="(item, index) in twoSubjectList" :key="index">
                  <a :title="item.title" href="#" :class="{active: twoIndex === index}"
                   @click="searchTwoSubject(item.id, index)">{{item.title}}</a>
                </li>
              </ul>
            </dd>
          </dl>
          <div class="clear"></div>
        </section>
        <div class="js-wrap">
          <section class="fr">
            <span class="c-ccc">
              <i class="c-master f-fM">1</i>/
              <i class="c-666 f-fM">1</i>
            </span>
          </section>
          <section class="fl">
            <ol class="js-tap clearfix">
              <li :class="{'current bg-orange': countSort !== ''}">
                <a title="关注度" href="#" @click="searchByCount">关注度&nbsp;
                  <span :class="{'hide': countSort === ''}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange': gmtCreateSort !== ''}">
                <a title="最新" href="#" @click="searchByGmtCreate">最新&nbsp;
                  <span :class="{'hide': gmtCreateSort === ''}">↓</span>
                </a>
              </li>
              <li :class="{'current bg-orange': priceSort !== ''}">
                <a title="价格" href="#" @click="searchByPrice">价格&nbsp;
                  <span :class="{'hide': priceSort === ''}">↓</span>
                </a>
              </li>
            </ol>
          </section>
        </div>
        <div class="mt40">
          <!-- /无数据提示 开始-->
          <section class="no-data-wrap" v-if="!data.total">
            <em class="icon30 no-data-ico">&nbsp;</em>
            <span class="c-666 fsize14 ml10 vam">没有相关数据，小编正在努力整理中...</span>
          </section>
          <!-- /无数据提示 结束-->
          <article class="comm-course-list" v-if="data.total">
            <ul class="of" id="bna">
              <li v-for="item in data.courseList" :key="item.id">
                <div class="cc-l-wrap">
                  <section class="course-img">
                    <img :src="item.cover" class="img-responsive" :alt="item.title">
                    <div class="cc-mask">
                      <a :href="'/course/' + item.id" title="开始学习" class="comm-btn c-btn-1">开始学习</a>
                    </div>
                  </section>
                  <h3 class="hLh30 txtOf mt10">
                    <a :href="'/course/' + item.id" :title="item.title" class="course-title fsize18 c-333">{{item.title}}</a>
                  </h3>
                  <section class="mt10 hLh20 of">
                    <span class="fr jgTag bg-green" v-if="!item.price">
                      <i class="c-fff fsize12 f-fA">免费</i>
                    </span>
                    <span class="fl jgAttr c-ccc f-fA">
                      <i class="c-999 f-fA">{{item.viewCount}}人学习</i>
                      |
                      <i class="c-999 f-fA">9194评论</i>
                    </span>
                  </section>
                </div>
              </li>
            </ul>
            <div class="clear"></div>
          </article>
        </div>
        <!-- 公共分页 开始 -->
        <div>
          <div class="paging">
            <!-- undisable这个class是否存在，取决于数据属性hasPrevious -->
            <!--这里还需要阻止a标签的默认行为-->
            <a href="#" :class="{undisable: !data.hasPrevious}" title="首页"
               @click.prevent="changePage(1)">首页</a>

            <a href="#" :class="{undisable: !data.hasPrevious}" title="前一页"
               @click.prevent="changePage(data.current - 1)">&lt;</a>

            <!--当前页等于遍历中的页码是则当前页不可点击且显示选中的效果-->
            <a href="#" :class="{current: data.current === item, undisable: data.current === item}"
               :title="'第' + item + '页'"
               v-for="(item,index) in data.pages"
               :key="index" @click.prevent="changePage(item)">{{item}}</a>

            <a href="#" :class="{undisable: !data.hasNext}" title="后一页"
               @click.prevent="changePage(data.current + 1)">&gt;</a>

            <a href="#" :class="{undisable: !data.hasNext}" title="末页"
               @click.prevent="changePage(data.pages)">末页</a>

            <div class="clear"></div>
          </div>
        </div>
        <!-- 公共分页 结束 -->
      </section>
    </section>
    <!-- /课程列表 结束 -->
  </div>
</template>
<script>
  import courseApi from '@/api/course'

  export default {
    data() {
      return {
        //当前页
        page: 1,
        //课程列表数据
        data: {},
        //一级分类列表
        oneSubjectList: [],
        //二级分类列表
        twoSubjectList: [],

        //查询条件
        queryInfo: {},

        oneIndex: -1,
        twoIndex: -1,
        countSort: '',
        gmtCreateSort: '',
        priceSort: '',
      }
    },
    created() {
      this.getCourseList();
      this.getSubjectList();
    },
    methods: {
      //查询课程信息
      async getCourseList() {
        const {data} = await courseApi.getCourseList(1, 8, this.queryInfo);
        this.data = data.data;
      },

      //查询所有的课程分类
      async getSubjectList() {
        const {data} = await courseApi.getAllSubject();
        this.oneSubjectList = data.data.data;
      },

      //分页切换方式以及条件查询
      async changePage(page) {
        const {data} = await courseApi.getCourseList(page, 8, this.queryInfo);
        this.data = data.data;
      },

      //点击一级分类后查询到对应的二级分类
      searchOneSubject(oneSubId, index) {
        this.oneIndex = index;
        this.twoIndex = -1;
        //this.queryInfo = {};
        //进行条件查询
        this.queryInfo.subjectParentId = oneSubId;
        this.changePage(1);

        //遍历所有的一级分类，找对对应的Id的一级分类，然后赋值children给二级分类
        for (let i = 0; i < this.oneSubjectList.length; i++) {
          if (this.oneSubjectList[i].id === oneSubId) {
            this.twoSubjectList = this.oneSubjectList[i].children;
          }
        }
      },

      //查询后端数据，显示对应的二级分类课程
      searchTwoSubject(twoSubId, index){
        this.twoIndex = index;
        this.queryInfo.subjectId = twoSubId;
        this.changePage(1);
      },

      //按照销售数量来排序
      searchByCount(){
        this.priceSort = '';
        this.gmtCreateSort = '';
        this.queryInfo.priceSort = '';
        this.queryInfo.gmtCreateSort= '';
        if (this.countSort){
          this.countSort = '';
        }else {
          this.countSort = 'countSort';
        }
        this.queryInfo.countSort = this.countSort;
        this.changePage(1);
      },

      //按照创建时间来排序
      searchByGmtCreate(){
        this.countSort = '';
        this.priceSort = '';
        this.queryInfo.countSort = '';
        this.queryInfo.priceSort = '';
        if (this.gmtCreateSort){
          this.gmtCreateSort = '';
        }else {
          this.gmtCreateSort = 'gmtCreateSort';
        }
        this.queryInfo.gmtCreateSort = this.gmtCreateSort;
        this.changePage(1);
      },

      //按价格排序
      searchByPrice(){
        this.countSort = '';
        this.gmtCreateSort = '';
        this.queryInfo.gmtCreateSort= '';
        this.queryInfo.countSort = '';
        if (this.priceSort){
          this.priceSort = '';
        }else {
          this.priceSort = 'priceSort';
        }
        this.queryInfo.priceSort = this.priceSort;
        this.changePage(1);
      },
    }
  };
</script>
<style scoped>
  .active{
    background: #ccc;
  }
  .hide{
    display: none;
  }
  .show{
    display: block;
  }
</style>
